# Contributor: lauren n. liberda <lauren@selfisekai.rocks>
# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=dart
# upgrade checklist: https://md.sdomi.pl/Q-ECZTJ2Qqyp3ahfJuYSBw
pkgver=3.5.4
pkgrel=0
_bootstrap=3.5.0_alpha278_p0
pkgdesc="Dart is a client-optimized language for fast apps on any platform"
url="https://dart.dev/"
arch="aarch64 armv7 x86_64"
license="BSD-3-Clause"
depends="ca-certificates"	# required for HTTPS requests, used by `dart pub get`
makedepends="
	curl
	dart-bootstrap>=$_bootstrap
	gn
	icu-dev
	linux-headers
	lld
	llvm
	python3
	ripgrep
	samurai
	zlib-dev
	zstd
	"
subpackages="$pkgname-sdk dartaotruntime"

case "$pkgver" in
	*.*.*_pre*)
		_canonver="${pkgver/_pre/-}"
		_canonver="${_canonver/-r/.}.dev"
		;;
	*.*.*_beta*)
		_canonver="${pkgver/_beta/-}"
		_canonver="${_canonver/-r/.}.beta"
		;;
	*.*.*)
		_canonver="$pkgver"
		;;
esac

source="
	https://ab-sn.lnl.gay/dart-sdk-$_canonver.tar.zst
	dart-wrapper.sh

	build-config.patch
	gcc13.patch
	no-werror.patch
	unbundle.patch
	where-we-are-heading-prefixes-are-not-needed.patch
	zlib-not-found.patch
	"
builddir="$srcdir/dart-sdk-$_canonver"

# gclient comes from teapot-tools
_distbucket="sakamoto/lnl-aports-snapshots/"
snapshot() {
	mkdir -p "$srcdir"
	cd "$srcdir"

	echo "
solutions = [{
	'name': 'sdk',
	'url': 'https://dart.googlesource.com/sdk.git@$_canonver',
}]
target_cpu = ['x64', 'arm64', 'arm', 'riscv64']
target_cpu_only = True
" > .gclient

	gclient sync --no-history --nohooks --tpot-cipd-ignore-platformed

	for elf in $(scanelf -RA -F "%F" sdk); do
		rm -f "$elf"
	done

	mv sdk dart-sdk-$_canonver

	msg "generating tarball.."
	tar -cf dart-sdk-$_canonver.tar \
		--exclude="ChangeLog*" \
		--exclude="sdk/buildtools/*/clang" \
		--exclude="third_party/fuchsia/sdk/linux/arch" \
		--exclude=".build-id" \
		--exclude-backups \
		--exclude-caches-all \
		--exclude-vcs \
		dart-sdk-$_canonver

	zstd --auto-threads=logical --ultra --long -22 -T0 -vv dart-sdk-$_canonver.tar -o "$SRCDEST"/dart-sdk-$_canonver.tar.zst
	mcli cp "$SRCDEST"/dart-sdk-$_canonver.tar.zst "$_distbucket"
}

case "$CARCH" in
	aarch64)
		_arch="arm64"
		_out="$builddir/out/ReleaseARM64"
		;;
	armv7)
		_arch="arm"
		_out="$builddir/out/ReleaseARM"
		;;
	riscv64)
		_arch="riscv64"
		_out="$builddir/out/ReleaseRISCV64"
		;;
	x86_64)
		_arch="x64"
		_out="$builddir/out/ReleaseX64"
		;;
esac

prepare() {
	default_prepare

	mkdir -p .git/logs
	echo '' > .git/logs/HEAD

	# bind bootstrapped interpreter
	rm -rf tools/sdks/dart-sdk
	ln -s /usr/lib/dart tools/sdks/dart-sdk

	ln -s /usr/bin/gn buildtools/gn
	mkdir -p buildtools/ninja
	ln -s /usr/bin/samu buildtools/ninja/ninja

	# gclient hooks
	python3 tools/generate_package_config.py
	python3 tools/generate_sdk_version_file.py

	# google analytics, doubleclick
	echo '' > tools/bots/dartdoc_footer.html
	rm third_party/devtools/web/devtools_analytics.js

	# disarm analytics for sure
	rg --no-ignore -l 'google-analytics\.com' . \
		| rg -v "\.map\$" \
		| xargs -t -n 1 -P ${JOBS:-2} sed -i -E 's|([^/]+\.)?google-analytics\.com|0\.0\.0\.0|g'
	rg --no-ignore -l 'UA-[0-9]+-[0-9]+' . \
		| xargs -t -n 1 -P ${JOBS:-2} sed -i -E 's|UA-[0-9]+-[0-9]+|UA-2137-0|g'

	sed -i 's/Unknown timestamp/'"$(date -uIm)"'/' tools/make_version.py

	# reusable system library settings
	local use_system="
		icu
		zlib
		"
	for _lib in $use_system; do
		msg "Removing buildscripts for system provided $_lib"
		find . -type f -path "*third_party/$_lib/*" \
			\! -path "*third_party/$_lib/chromium/*" \
			\! -path "*third_party/$_lib/google/*" \
			\! -regex '.*\.\(gn\|gni\|isolate\|py\)' \
			-delete
	done
	msg "Replacing gn files"
	python3 build/linux/unbundle/replace_gn_files.py --system-libraries \
		$use_system
}

build() {
	# shellcheck disable=2089
	local gn_args="
		create_kernel_service_snapshot=true
		dart_snapshot_kind=\"app-jit\"
		dart_sysroot=\"\"
		dart_use_tcmalloc=false
	"

	msg "Building"
	# shellcheck disable=2116,2090
	python3 ./tools/build.py \
		--no-clang \
		--arch="$_arch" \
		--mode=release \
		--no-verify-sdk-hash \
		--gn-args="$(echo $gn_args)" \
		create_sdk runtime
}

check() {
	# check sanity
	"$_out"/dart-sdk/bin/dart --version
}

package() {
	mkdir -p "$pkgdir"/usr/bin "$pkgdir"/usr/lib "$pkgdir"/usr/include

	cp -r "$_out"/dart-sdk "$pkgdir"/usr/lib/dart

	ln -s ../lib/dart/include "$pkgdir"/usr/include/dart
	install -Dm755 "$srcdir"/dart-wrapper.sh "$pkgdir"/usr/bin/dart
	ln -s ../lib/dart/bin/dartaotruntime "$pkgdir"/usr/bin/dartaotruntime

	find "$pkgdir"/usr/lib/dart/bin/resources/devtools -type f -exec chmod 644 {} \;
}

dartaotruntime() {
	amove usr/bin/dartaotruntime
	amove usr/lib/dart/bin/dartaotruntime
}

sdk() {
	# do the stuff -dev does by default here
	default_dev

	# must be set after default_dev or gets cleared
	depends="dart=$pkgver-r$pkgrel dartaotruntime=$pkgver-r$pkgrel"
	provides="dart-bootstrap=$pkgver-r$pkgrel dart-devtools=$pkgver-r$pkgrel"
	provider_priority=100

	_snapshots="
		analysis_server
		dart2js
		dartdevc
		gen_kernel_aot
		kernel-service
		kernel_worker
	"
	for snap in $_snapshots; do
		amove usr/lib/dart/bin/snapshots/$snap.dart.snapshot
	done
	amove usr/lib/dart/bin/resources/dartdoc
	amove usr/lib/dart/bin/resources/devtools
	amove usr/lib/dart/bin/snapshots/dart2wasm_product.snapshot
	amove usr/lib/dart/bin/utils
	amove usr/lib/dart/lib
}

sha512sums="
c01619fb0b9608dae28a19766784a10442deef416eefb5ab6442dfc3a2f20745d358d893a114e6c2e310fc7c5808cdac9aea3763cd760e92b6604e505f744951  dart-sdk-3.5.4.tar.zst
5ccd4a87d4494256c1fc7593164e2b1b7195e386f66344aec64ad6f9a344947bc32755dc70ae9f42044fdb81fdde88c8418b5db30ee9bce7bd293812b4c7891f  dart-wrapper.sh
3ae8364a172b029cf0b45cd4fd1ac192a20772c84924bb59a1775339ed6314cf81c398b078823fd9f07abc1b0de4b5704c76ce84c64f8924e645f00cfcda4ff3  build-config.patch
03562a3fa935b6819458acdfcadf24f71accf73e2000d158c1fe41aca068b21fb158350a32911c322672b592beefea1e9c7e4675a98418804e60000049a656c8  gcc13.patch
e7f5c715d9ed01c36e8b60892332b1bc763a3f98ca87905e2102491c469db60ab21dba9b3e116e1a7ec3faef7d65b8fdd60d7148cfb848ef5a2b411a06f425cf  no-werror.patch
14b6b024688110631dfce6d51b895e84d4306a00e66c0e5306126f6d27cbd13894bf2c50faffab99e57107901ec3977a4b0038bee9e165f733ef9e4ccc14af1a  unbundle.patch
c73ca1993fab3696563a0ac6a95010da507cc774bdbf2489d71dd7a69959ec166e88e05a199cd5727225b7165aeb44561887f24c8d328953fde1c262fbafd862  where-we-are-heading-prefixes-are-not-needed.patch
460eedf708e8093e0484e3d13fc61d891f4b3c1f8c8595c962410550ef988773aea87173e3fe60626a687dcacbc7e3c531d811bbd96a8856a0c1694d5c53e4b0  zlib-not-found.patch
"
